//
//  PoliceResidencePermitViewController.m
//  Jyxt
//
//  Created by 王炜光 on 2017/8/17.
//  Copyright © 2017年 DaLian Rental Housing Management. All rights reserved.
//

#import "PoliceResidencePermitViewController.h"
#import "ReviewResidencePermitDelegate.h"
#import "ReviewResidencePermitDataSource.h"
#import "PassedResidencePermitDelegate.h"
#import "PassedResidencePermitDataSource.h"
#import "RefuseResidencePermitDelegate.h"
#import "RefuseResidencePermitDataSource.h"
#import "ResidencePermitViewModel.h"
#import "UserCenterViewModel.h"
#import <LemonBubble.h>
#import <MJRefresh.h>

@interface PoliceResidencePermitViewController ()<UIScrollViewDelegate>

@property (nonatomic , strong) ReviewResidencePermitDelegate *reviewResidencePermitDelegate;
@property (nonatomic , strong) ReviewResidencePermitDataSource *reviewResidencePermitDataSource;

@property (nonatomic , strong) PassedResidencePermitDelegate *passedResidencePermitDelegate;
@property (nonatomic , strong) PassedResidencePermitDataSource *passedResidencePermitDataSource;

@property (nonatomic , strong) RefuseResidencePermitDelegate *refuseResidencePermitDelegate;
@property (nonatomic , strong) RefuseResidencePermitDataSource *refuseResidencePermitDataSource;
/**
 居住证tableView
 */
@property (nonatomic, strong)UITableView *reviewTableView;
@property (nonatomic, strong)UITableView *passedTableView;
@property (nonatomic, strong)UITableView *refusedTableView;

/**
 标题label
 */
@property (nonatomic, strong)UILabel *titleLabel;

/**
 滑动scrollView
 */
@property (nonatomic, strong)UIScrollView *bgScrollView;

/**
 蓝色头部试图
 */
@property (nonatomic, strong)UIView *headBGView;

/**
 白色线
 */
@property (nonatomic, strong)UIView *lineView;

/**
 数据源dataArr
 */
@property (nonatomic, strong)NSMutableArray *reviewArr;
@property (nonatomic, strong)NSMutableArray *passedArr;
@property (nonatomic, strong)NSMutableArray *refuseArr;

/**
 网络请求的数据分页
 */
@property (nonatomic, assign)NSInteger reviewInt;
@property (nonatomic, assign)NSInteger passedInt;
@property (nonatomic, assign)NSInteger refuseInt;

@end

@implementation PoliceResidencePermitViewController


- (void)initView{
    
    _reviewArr = [[NSMutableArray alloc] init];
    _passedArr = [[NSMutableArray alloc] init];
    _refuseArr = [[NSMutableArray alloc] init];
    
    UIButton*rightButton = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-50,30,30,30)];
//    [rightButton setTitle:@"查询" forState:UIControlStateNormal];
    [rightButton setImage:[UIImage imageNamed:@"m_common_search"] forState:UIControlStateNormal];
    [rightButton.titleLabel setFont:[UIFont systemFontOfSize:11]];
    [rightButton addTarget:self action:@selector(search:) forControlEvents:UIControlEventTouchUpInside];
    [rightButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    _headBGView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 106)];
    _headBGView.backgroundColor = LKColorMake(100,131,228, 1);
    [self.view addSubview:_headBGView];
    
    [_headBGView addSubview:rightButton];
    
    _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, SCREEN_WIDTH, 44)];
    _titleLabel.textColor = [UIColor whiteColor];
    _titleLabel.text = @"居住证信息";
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    [_headBGView addSubview:_titleLabel];
    
    NSArray *buttonTitleArr = @[@"待审核",@"已通过",@"已拒绝"];
    for (NSInteger i = 0; i < buttonTitleArr.count; i++) {
        UIButton *tempBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [tempBtn setTitle:buttonTitleArr[i] forState:UIControlStateNormal];
        tempBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [tempBtn setTitleColor:LKColorMake(160, 179, 234, 1) forState:UIControlStateNormal];
        tempBtn.frame = CGRectMake(0, 0, SCREEN_WIDTH/3, 40);
        tempBtn.center = CGPointMake((2*i+1)*SCREEN_WIDTH/6, 84);
        [tempBtn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        tempBtn.tag = 1000+i;
        [_headBGView addSubview:tempBtn];
        if (i == 0) {
            [tempBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        }
    }
    
    _lineView = [[UIView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/6/2, 102, SCREEN_WIDTH/6, 2)];
    _lineView.backgroundColor = [UIColor whiteColor];
    [_headBGView addSubview:_lineView];
    
    _bgScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 106, SCREEN_WIDTH, SCREEN_HEIGHT-155)];
    _bgScrollView.pagingEnabled = YES;
    _bgScrollView.contentSize = CGSizeMake(SCREEN_WIDTH*3, 0);
    _bgScrollView.delegate = self;
    [_bgScrollView setShowsHorizontalScrollIndicator:NO];
    [self.view addSubview:_bgScrollView];
    
    _reviewTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-155) style:UITableViewStylePlain];
    _reviewTableView.delegate = self.reviewResidencePermitDelegate;
    _reviewTableView.dataSource = self.reviewResidencePermitDataSource;
    _reviewTableView.tableFooterView = [[UIView alloc]init];
    [_bgScrollView addSubview:_reviewTableView];
    
    //默认block方法：设置下拉刷新
    _reviewTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _reviewInt=1;
        [_reviewArr removeAllObjects];
        [self loadDataPageNo:[NSString stringWithFormat:@"%ld",_reviewInt] Status:@"1"];
    }];
    
    _passedTableView = [[UITableView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH, 0, SCREEN_WIDTH, SCREEN_HEIGHT-155) style:UITableViewStylePlain];
    _passedTableView.delegate = self.passedResidencePermitDelegate;
    _passedTableView.dataSource = self.passedResidencePermitDataSource;
    _passedTableView.tableFooterView = [[UIView alloc]init];
    [_bgScrollView addSubview:_passedTableView];
    
    //默认block方法：设置下拉刷新
    _passedTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _passedInt=1;
        [_passedArr removeAllObjects];
        [self loadDataPageNo:[NSString stringWithFormat:@"%ld",_passedInt] Status:@"2"];
    }];
    
    _refusedTableView = [[UITableView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH*2, 0, SCREEN_WIDTH, SCREEN_HEIGHT-106) style:UITableViewStylePlain];
    _refusedTableView.delegate = self.refuseResidencePermitDelegate;
    _refusedTableView.dataSource = self.refuseResidencePermitDataSource;
    _refusedTableView.tableFooterView = [[UIView alloc]init];
    [_bgScrollView addSubview:_refusedTableView];
    
    //默认block方法：设置下拉刷新
    _refusedTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _refuseInt=1;
        [_refuseArr removeAllObjects];
        [self loadDataPageNo:[NSString stringWithFormat:@"%ld",_refuseInt] Status:@"3"];
    }];
}

- (void)loadDataPageNo:(NSString *)pageNo Status:(NSString *)status{
    
    NSString *dataStr = [ResidencePermitViewModel dictionaryToJson:@{@"pageNo":pageNo,@"pageSize":@"10",@"orderBy":@""}];
    NSLog(@"%@",dataStr);
    [ResidencePermitViewModel getResidencePermitListWithData:@{@"utoken":[UserCenterViewModel getLocalUToken],@"data":dataStr,@"status":status}success:^(id responce){
        
        switch ([status integerValue]) {
            case 1:
                [_reviewArr addObjectsFromArray:responce[@"data"]];
//                if ([responce[@"lastPage"] boolValue]) {
                if (((NSArray *)responce[@"data"]).count == 0) {
//                    _reviewTableView.mj_footer = nil;
                    [self showRightWithTitle: @"已无更多数据" autoCloseTime: 2];
                }else{
                    _reviewTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
                        //Call this Block When enter the refresh status automatically
                        _reviewInt++;
                        [self loadDataPageNo:[NSString stringWithFormat:@"%ld",_reviewInt] Status:@"1"];
                    }];
                }
                [_reviewTableView.mj_footer endRefreshing];
                [_reviewTableView.mj_header endRefreshing];
                [_reviewTableView reloadData];
                break;
            case 2:
                [_passedArr addObjectsFromArray:responce[@"data"]];
//                if ([responce[@"lastPage"] boolValue]) {
                if (((NSArray *)responce[@"data"]).count == 0) {
//                    _passedTableView.mj_footer = nil;
                    [self showRightWithTitle: @"已无更多数据" autoCloseTime: 2];
                }else{
                    _passedTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
                        //Call this Block When enter the refresh status automatically
                        _passedInt++;
                        [self loadDataPageNo:[NSString stringWithFormat:@"%ld",_passedInt] Status:@"2"];
                    }];
                }
                [_passedTableView.mj_footer endRefreshing];
                [_passedTableView.mj_header endRefreshing];
                [_passedTableView reloadData];
                break;
            case 3:
                [_refuseArr addObjectsFromArray:responce[@"data"]];
//                if ([responce[@"lastPage"] boolValue]) {
                if (((NSArray *)responce[@"data"]).count == 0) {
//                    _refusedTableView.mj_footer = nil;
                    [self showRightWithTitle: @"已无更多数据" autoCloseTime: 2];
                }else{
                    _refusedTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
                        //Call this Block When enter the refresh status automatically
                        _refuseInt++;
                        [self loadDataPageNo:[NSString stringWithFormat:@"%ld",_refuseInt] Status:@"3"];
                    }];
                }
                [_refusedTableView.mj_footer endRefreshing];
                [_refusedTableView.mj_header endRefreshing];
                [_refusedTableView reloadData];
                break;
                
            default:
                break;
        }
        
    } failed:^(NSString *reason) {
        [self showErrorWithTitle: reason autoCloseTime: 3];
        [_refusedTableView.mj_footer endRefreshing];
        [_refusedTableView.mj_header endRefreshing];
        [_passedTableView.mj_footer endRefreshing];
        [_passedTableView.mj_header endRefreshing];
        [_reviewTableView.mj_footer endRefreshing];
        [_reviewTableView.mj_header endRefreshing];
    }];
}

- (void)search:(UIButton *)btn{
    [LKFreeCtrl pushViewController:[LKInstance findViewController:@"SearchResidencePermitViewController"]];
}

- (void)buttonAction:(UIButton *)btn{
    [_bgScrollView setContentOffset:CGPointMake((btn.tag-1000) * SCREEN_WIDTH, 0) animated:YES];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.lkNavigationBar.hidden = YES;
    if ([UserCenterViewModel getLocalUToken]) {
        [_reviewArr removeAllObjects];
        [_passedArr removeAllObjects];
        [_refuseArr removeAllObjects];

        [_reviewTableView.mj_header beginRefreshing];
        [_passedTableView.mj_header beginRefreshing];
        [_refusedTableView.mj_header beginRefreshing];
        
    }
    
}

- (ReviewResidencePermitDataSource *)reviewResidencePermitDataSource{
    if (!_reviewResidencePermitDataSource) {
        _reviewResidencePermitDataSource = [ReviewResidencePermitDataSource new];
    }
    _reviewResidencePermitDataSource.dataArr = _reviewArr;
    return _reviewResidencePermitDataSource;
}

- (ReviewResidencePermitDelegate *)reviewResidencePermitDelegate{
    if (!_reviewResidencePermitDelegate) {
        _reviewResidencePermitDelegate = [ReviewResidencePermitDelegate new];
    }
    _reviewResidencePermitDelegate.dataArr = _reviewArr;
    return _reviewResidencePermitDelegate;
}

- (PassedResidencePermitDataSource *)passedResidencePermitDataSource{
    if (!_passedResidencePermitDataSource) {
        _passedResidencePermitDataSource = [PassedResidencePermitDataSource new];
    }
    _passedResidencePermitDataSource.dataArr = _passedArr;
    return _passedResidencePermitDataSource;
}

- (PassedResidencePermitDelegate *)passedResidencePermitDelegate{
    if (!_passedResidencePermitDelegate) {
        _passedResidencePermitDelegate = [PassedResidencePermitDelegate new];
    }
    _passedResidencePermitDelegate.dataArr = _passedArr;
    return _passedResidencePermitDelegate;
}

- (RefuseResidencePermitDataSource *)refuseResidencePermitDataSource{
    if (!_refuseResidencePermitDataSource) {
        _refuseResidencePermitDataSource = [RefuseResidencePermitDataSource new];
    }
    _refuseResidencePermitDataSource.dataArr = _refuseArr;
    return _refuseResidencePermitDataSource;
}

- (RefuseResidencePermitDelegate *)refuseResidencePermitDelegate{
    if (!_refuseResidencePermitDelegate) {
        _refuseResidencePermitDelegate = [RefuseResidencePermitDelegate new];
    }
    _refuseResidencePermitDelegate.dataArr = _refuseArr;
    return _refuseResidencePermitDelegate;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    _lineView.center = CGPointMake(scrollView.contentOffset.x/3+SCREEN_WIDTH/6, 103);

    for (NSInteger i = 0 ; i < 3; i++) {
        UIButton *tempBtn = [self.view viewWithTag:1000+i];
        if (i != scrollView.contentOffset.x/SCREEN_WIDTH) {
            [tempBtn setTitleColor:LKColorMake(160, 179, 234, 1) forState:UIControlStateNormal];
        }else{
            [tempBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        }
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
